﻿using Microsoft.AspNetCore.Mvc;
using MyProject;
namespace Controllers{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserInfoController : BaseController
    {
        public IFreeSql _fsql;

        public UserInfoController(IFreeSql fsql)
        {
            _fsql = fsql;
        }
        [HttpPost]
        public ApiModel Add([FromBody] Userinfo data)
        {
            long id = _fsql.Insert(data).ExecuteIdentity();
            data.Id = (int)id;
            if (id==0)
            {
                return apiModel(Status.Error, "插入错误");
            }
            else
            {
                return apiModel(Status.Success, "", data);
            }
        }
        [HttpPost]
        //[Authorize(Roles ="user")] 
        public ApiModel Get([FromBody] Userinfo data)
        {
            var t1 = _fsql.Select<Userinfo>().Where(a=>a.Id==data.Id).ToOne();
            if (t1 == null)
            {
                return apiModel(Status.Error, "无数据");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        [HttpPost]
        public ApiModel Delete([FromBody] Userinfo data)
        {
            var t1 = _fsql.Delete<Userinfo>(new { id = data.Id }).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "删除错误");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
        [HttpPost]
        public ApiModel Update([FromBody] Userinfo data)
        {
            var t1 = _fsql.Update<Userinfo>().SetSourceIgnore(data, col => col == null).Where(a => a.Id == data.Id).ExecuteAffrows();
            if (t1 == 0)
            {
                return apiModel(Status.Error, "更新错误");
            }
            else
            {
                return apiModel(Status.Success, "", t1);
            }
        }
    }
}